package ru.spb.osll.myfit.server.parser;

import ru.spb.osll.myfit.common.Page;
import ru.spb.osll.myfit.common.PageTag;
import ru.spb.osll.myfit.common.TextParsingTools;

import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * User: Artem Bukhonov
 * Date: 24.02.12
 * Time: 17:49
 */
public class MetadataParser {
  private static final Pattern TAGS_PATTERN = Pattern.compile(TextParsingTools.TAGS_PROPERTY_NAME + "=(.*)");

  public Set<PageTag> parseMetadata(List<String> metadataContent) {
    Set<PageTag> result = new TreeSet<PageTag>();
    for (String line : metadataContent) {
      Matcher matcher = TAGS_PATTERN.matcher(line);
      if (matcher.matches()) {
        String[] tags = matcher.group(1).split(",");
        for (String tag : tags) {
          tag = tag.trim();
          if (!tag.equals(""))
            result.add(new PageTag(tag));
        }
      }
    }
    return result;
  }
}
